Subject: Re: Allocating Mem & Loading
From: [email-masked] (Doug Walker)
Date: Mon, 13 Jun 1994 12:52:17 GMT
Newsgroups: comp.sys.amiga.programmer

In article <[email-masked]>, [email-masked] writes:
|> I would like to allocate memory for an array of a structure.  The number of the
|> array will be deterimined by my data file, so it is not the same everytime. 
|> Well the question is how can I load the entire file into this array without
|> having a loop reading in one set of the array at a time.  I want to do one read
|> statement to load the entire file into the array of strutures.. 
|>  
|> Example:
|>  
|> long bytes,value;
|>  
|> bytes = FileSize(file);
|> value = bytes / sizeof struct(A);
|>  
|> - then allocate the memory for however many array's we need (Value) and then
|>   I want to do a single read command to load the entire file into that array
|>  
|> Does anyone know how I could do this?  thanks

This is somewhat SAS/C dependent, but it might work with the new
commercial DICE as well (I think it has the __aligned keyword):

   #include <proto/dos.h>
   #include <stdio.h>

   int FileSize(char *filename)
   {
      __aligned struct FileInfoBlock fib;
      BPTR fh;
      int size = -1;
   
      if(fh = Lock(filename, SHARED_LOCK))
      {
         if(Examine(fh, &fib) == DOSTRUE)
         {
            size = fib.fib_Size;
         }
         UnLock(fh);
      }
      return size;
   }

The following version should work with any C compiler:

   #include <clib/dos_protos.h>
   #include <stdio.h>
   
   int FileSize(char *filename)
   {
      struct FileInfoBlock *fib;
      BPTR fh;
      int size = -1;
      
      fib = malloc(sizeof(struct FileInfoBlock));
      if(fib == NULL) return size;
      
      if(fh = Lock(filename, SHARED_LOCK))
      {
         if(Examine, fh, fib) == DOSTRUE)
         {
            size = fib->fib_Size;
         }
         UnLock(fh);
      }
      return size;
   }

Your single read command is an fread().  Just allocate a buffer the
size you need (using malloc()) and then fread() into the buffer.

-- 
  *****                    / [email-masked]
 *|_o_o|\\     Doug Walker<  BIX, Portal: djwalker
 *|. o.| ||                \ CompuServe: 71165,2274
  | o  |//     
  ====== 
Any opinions expressed are mine, not those of SAS Institute, Inc.